<template>
  <div>
    <div class="view-title">活跃用户</div>
    <div class="select_box">
      <div>
        <el-select v-model="checkDays.label" @change="handleChange">
          <el-option
            v-for="item in activeOptions"
            :key="Number(item.value)"
            :label="item.label"
            :value="Number(item.value)"
          >
          </el-option>
        </el-select>
      </div>
    </div>
    <div id="actEChart" :style="{ width: '100%', height: '300px' }"></div>
    <div class="view-title" style="margintop: 20px; padding-top: 20px">
      活跃用户列表
    </div>
    <!-- table -->
    <div class="table_box">
      <el-table :data="activePgeTableData" style="width: 100%">
        <el-table-column prop="date" label="日期" align="center">
        </el-table-column>
        <el-table-column prop="count" label="活跃用户量" align="center">
        </el-table-column>
      </el-table>
    </div>
    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getActivateUsercount } from "@/api/activeUsers";
import echarts from "echarts";
require("echarts/theme/macarons");
export default {
  data() {
    return {
      chart: null,

      checkDays: {
        value: "7",
        label: "近7天",
      },
      activeOptions: [
        {
          value: "3",
          label: "近3天",
        },
        {
          value: "7",
          label: "近7天",
        },
        {
          value: "14",
          label: "近14天",
        },
        {
          value: "30",
          label: "近30天",
        },
      ],
      activeTableData: [],
      activePgeTableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.handle);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.handle);
  },
  methods: {
    handle() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(document.getElementById("actEChart"));
      this.getActivateUsercount();
    },
    // 查询用户活跃度
    getActivateUsercount() {
      getActivateUsercount({
        select_day: this.checkDays.value - 1,
      }).then((res) => {
        if (res.code == 1) {
          const dateList = [];
          const userNumList = [];
          res.data.forEach((item) => {
            dateList.push(item.date);
            userNumList.push(item.count);
          });

          // 配置参数
          let config = {
            dataZoom: [
              {
                // 设置滚动条的隐藏与显示
                show: true,
                // 设置滚动条类型
                type: "slider",
                // 设置背景颜色
                backgroundColor: "#eee",
                // 设置选中范围的填充颜色
                fillerColor: "#ccc",
                // 设置边框颜色
                // borderColor: "rgb(19, 63, 100)",
                // 是否显示detail，即拖拽时候显示详细数值信息
                showDetail: false,
                // 数据窗口范围的起始数值
                startValue: 0,
                // 数据窗口范围的结束数值（一页显示多少条数据）
                endValue: 8,
                // empty：当前数据窗口外的数据，被设置为空。
                // 即不会影响其他轴的数据范围
                filterMode: "empty",
                // 设置滚动条宽度，相对于盒子宽度
                width: "80%",
                // 设置滚动条高度
                height: 8,
                // 设置滚动条显示位置
                left: "center",
                // 是否锁定选择区域（或叫做数据窗口）的大小
                zoomLoxk: true,
                // 控制手柄的尺寸
                handleSize: 0,
                // dataZoom-slider组件离容器下侧的距离
                bottom: 3,
              },
              {
                // 没有下面这块的话，只能拖动滚动条，
                // 鼠标滚轮在区域内不能控制外部滚动条
                type: "inside",
                // 滚轮是否触发缩放
                zoomOnMouseWheel: false,
                // 鼠标滚轮触发滚动
                moveOnMouseMove: true,
                moveOnMouseWheel: true,
              },
            ],

            tooltip: {
              trigger: "axis",
            },
            legend: {
              data: ["活跃用户"],
            },
            grid: {
              left: "3%",
              right: "4%",
              bottom: "3%",
              containLabel: true,
            },
            xAxis: {
              type: "category",
              boundaryGap: false,
              data: dateList,
            },
            yAxis: {
              type: "value",
            },
            series: [
              {
                name: "活跃用户",
                type: "line",
                data: userNumList,
                smooth: true,
                label: {
                  show: true,
                  position: "top",
                  color: "#000",
                },
              },
            ],
          };
          // 设置参数
          this.chart.setOption(config);

          this.activeTableData = res.data.reverse();
          this.activePgeTableData = this.queryByPage();
          this.total = res.data.length;
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 选择天数
    handleChange(e) {
      this.checkDays.value = e;
      this.getActivateUsercount();
    },

    // 实现分页的方法
    queryByPage() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = this.currentPage * this.pageSize;
      return this.activeTableData.slice(start, end);
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.activePgeTableData = this.queryByPage();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.activePgeTableData = this.queryByPage();
    },
  },
};
</script>
<style lang="scss" scoped>
.select_box {
  display: flex;
  justify-content: flex-end;
}

.table_box {
  padding: 10px 20px;
}
</style>
